#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
清算 - 提现申请功能使用示例
"""

import os
import sys

# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from examples.base_example import BaseExample
from jlpay.model.clearing import WithdrawApplyRequest
from jlpay.services.clearing_service import ClearingService


class WithdrawApplyExample(BaseExample):
    """提现申请示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.clearing.withdraw.apply")
        # 创建ClearingService实例
        self.clearing_service = ClearingService(self.config)
    
    def build_withdraw_apply_request(self):
        """构建提现申请请求"""
        request = WithdrawApplyRequest()
        
        # 设置请求参数
        request.set_org_no("123456")
        request.set_merch_no("849584358120018")  # T0结算商户
        request.set_amount("10")
        request.set_settle_type("T0")
        request.set_out_settle_id("1234567890128")
        request.set_notify_url("https://www.baidu.com")
        request.set_postscript("附言")
        
        return request
    
    def demo(self):
        """执行提现申请示例"""
        self.logger.info("开始执行提现申请示例")
        
        # 创建提现申请请求
        request = self.build_withdraw_apply_request()
        
        # 发送请求并处理响应
        response = self.send_request_and_handle_response(
            self.clearing_service.apply_withdraw,
            "提现申请",
            request
        )
        
        # 处理响应结果
        if response and response.is_success():
            self.logger.info(f"响应报文：{response.to_dict()}")
        
        self.logger.info("提现申请示例执行完毕")


if __name__ == "__main__":
    logger = WithdrawApplyExample().logger
    logger.info("==================================================")
    logger.info("===== 提现申请示例 开始 =====")
    logger.info("==================================================")
    
    example = WithdrawApplyExample()
    example.demo()
    
    logger.info("==================================================")
    logger.info("===== 提现申请示例执行完毕 =====")
    logger.info("==================================================") 